
from typing import Text
from . import Base
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime,JSON,Text,DECIMAL,BigInteger
from sqlalchemy.orm import backref, relationship
from datetime import datetime
from .base import SnowFlakeIdModel
import uuid

class Commodity(Base,SnowFlakeIdModel):
    __tablename__ = 'commodity'
    title=Column(String(255), nullable=False)
    price=Column(DECIMAL(10,2))
    covers=Column(JSON)
    detail=Column(Text)
    created_time=Column(DateTime, default=datetime.now)

class Seckill(Base,SnowFlakeIdModel):
    __tablename__ = 'seckill'
    sk_price=Column(DECIMAL(10,2),comment='秒杀价格')
    start_time=Column(DateTime,comment='秒杀开始时间')
    end_time=Column(DateTime,comment='秒杀结束时间')
    create_time=Column(DateTime,default=datetime.now,comment='创建时间')
    max_sk_count=Column(Integer,comment='最大秒杀数量')
    sk_per_max_count=Column(Integer,comment='每秒最大秒杀数量')
    stock=Column(Integer,comment='库存')
    version_id=Column(String(100),nullable=False,comment='版本id')
    
    commondity_id=Column(BigInteger,ForeignKey('commodity.id'),comment='商品id')
    commodity=relationship('Commodity',backref=backref('Seckill'),lazy='select')

    __mapper_args__={
        "version_id_col":version_id,
        "version_id_generator":lambda version:uuid.uuid4().hex,
    }